package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.Refinery;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.BRefinery;
import cn.eonshine.cuba.lsms.modular.basic.entity.Office;
import cn.eonshine.cuba.lsms.modular.basic.service.IBRefineryService;
import cn.eonshine.cuba.lsms.modular.basic.service.IOfficeService;
import cn.eonshine.cuba.lsms.modular.basic.service.IRefineryService;

@Service
public class RefineryService extends CommonService<Refinery> implements IRefineryService{
	@Autowired
	private IOfficeService officeService;
	
	@Autowired
	private IBRefineryService bRefineryService;
	
	private final String listUrl = "/refinery/list";
	private final String updateUrl = "/refinery/update";
	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.eonshine.cuba.inf.modular.lsms.basic.service.IInventoryDataService#
	 * requestInventoryData()
	 */
	@Override
	public void requestRefinery() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.REFINERY_NUM);
	}
	
	@Override
	protected Object dataHandle(Refinery refinery) throws Exception {
		QueryWrapper<Office> entity = new QueryWrapper<Office>();
		Office info = null;
		if(StringUtils.isBlank(refinery.getId())) {
			entity.eq("CODE", refinery.getCode());
			entity.eq("TYPE", InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_REFINERY);
			info = officeService.getOne(entity);
		}else {
			info = officeService.getById(refinery.getId());
		}

		boolean isInsert = false;
		if (info == null) {
			info = new Office();
			isInsert = true;
		}
		info.setType(InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_REFINERY);
		info.setCode(StringUtils.trim(refinery.getCode()));
		info.setName(StringUtils.trim(refinery.getName()));
		info.setFullName(StringUtils.trim(refinery.getFullName()));
		info.setUseable(refinery.getUseable());
		info.setIsFromInf(InfStatus.IS_FROM_INTERFACE);
		if(isInsert) {
			info.setAddress(refinery.getAddress());
			info.setMaster(refinery.getMaster());
			info.setPhone(refinery.getPhone());
			info.setEmail(refinery.getEmail());
			info.setParentId(InfStatus.LSMS_OFFICE_PARENT.ROOT);
			info.setParentIds(InfStatus.LSMS_OFFICE_PARENT.ROOT+InfStatus.SEPARATOR_CHAR);
			info.setCreateBy(InfStatus.CREATE_BY);
			if(info.getCreateDate() == null) {
				info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
			}
		}
		
		Date nowTime = DateUtils.getCubaDate(Calendar.getInstance().getTime());
		info.setUpdateBy(InfStatus.UPDATE_BY);
		info.setUpdateDate(nowTime);
		
		if (isInsert) {
			officeService.save(info);
			BRefinery bRefinery = new BRefinery();
			bRefinery.setId(info.getId());
			bRefineryService.save(bRefinery);
		} else {
			officeService.updateById(info);
		}
		return info;
	}
	@Override
	protected List<Refinery> feadBackDataHandle(Object obj, Refinery refinery, List<Refinery> feadBackDataLists) throws Exception {
		Office info = (Office) obj;
		Refinery tmpBack = new Refinery();
		tmpBack.setCode(info.getCode());
		tmpBack.setName(info.getName());
		tmpBack.setDataInputDatetime(refinery.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		tmpBack.setId(info.getId());
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
